package view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;

/**
 * Created by Administrator on 2017/5/18.
 * 下拉刷新的头部
 */

public class MyStoreHouseHeader extends LinearLayout implements PtrUIHandler {
    private View view;
    private Context context;
    private ImageView ima;
    private TextView textView;
    private  AnimationDrawable animationDrawable;

    public MyStoreHouseHeader(Context context) {
        super(context);
        this.context = context;
        initView();
    }

    public MyStoreHouseHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public MyStoreHouseHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    private void initView() {
        view = LayoutInflater.from(context).inflate(R.layout.my_store_house_header, this, false);
        addView(view);
        ima = (ImageView) view.findViewById(R.id.my_store_house_header_ima);
        textView = (TextView) view.findViewById(R.id.my_store_house_header_tv);
        animationDrawable = (AnimationDrawable) ima.getDrawable();

    }

    @Override
    public void onUIReset(PtrFrameLayout frame) {

    }

    @Override
    public void onUIRefreshPrepare(PtrFrameLayout frame) {
        //准备刷新
        textView.setText("下拉可以刷新");
        animationDrawable.start();
    }

    @Override
    public void onUIRefreshBegin(PtrFrameLayout frame) {

    }

    @Override
    public void onUIRefreshComplete(PtrFrameLayout frame) {
        textView.setText("刷新完成");
        animationDrawable.stop();
    }


    @Override
    public void onUIPositionChange(final PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
        if (frame.isRefreshing()) {
            //刷新中
            textView.setText("正在刷新。。。");
        }
        final int mOffsetToRefresh = frame.getOffsetToRefresh();
        final int currentPos = ptrIndicator.getCurrentPosY();

        if (currentPos < mOffsetToRefresh) {
            //未到达刷新线
            if (status == PtrFrameLayout.PTR_STATUS_PREPARE) {
                textView.setText("下拉刷新");
            }
        } else if (currentPos > mOffsetToRefresh) {
            //到达或超过刷新线
            if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
                textView.setText("释放刷新");
            }
        }

    }
}
